/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.lua;

import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.object.GameObject;
import se.krka.kahlua.vm.JavaFunction;
import se.krka.kahlua.vm.LuaCallFrame;


public class GameObjectFunctions {
    public static class Remove implements JavaFunction
    {

        public int call(LuaCallFrame callFrame, int nArguments) {
            final GameObject obj = (GameObject) callFrame.get(0);
            Runnable r = new Runnable()
            {
                public void run() {
                    try {
                        obj.remove();
                    } catch (WorldsException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            };
            Main.runInMainThread(r);
            return 0;
        }
    }
    public static class IsA implements JavaFunction
    {

        public int call(LuaCallFrame callFrame, int nArguments) {
            GameObject obj = (GameObject) callFrame.get(0);
            if(nArguments != 2 || !(callFrame.get(1) instanceof String))
                throw new RuntimeException("Please specify an object type");
            String t = callFrame.get(1).toString();
            try {
                callFrame.push(obj.getStringProp("Type").equals(t));
            } catch (WorldsException ex) {
                throw new RuntimeException(ex);
            }
            return 1;
        }
    }
}
